package com.apartment.sevice.impl;

import com.apartment.dao.UserDao;
import com.apartment.data.entity.User;
import com.apartment.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Author : xuzhiyong
 * Date : 2022/5/27 22:52
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public User userLogin(String username, String password) {
        return userDao.findByUsernameAndPassword(username,password);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        try {
            userDao.save(user);
            flag = true;
        }catch (Exception e){
            System.out.println("添加普通用户失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteUserById(Integer id) {
        boolean flag = false;
        try {
            userDao.deleteById(id);
            flag = true;
        }catch (Exception e){
            System.out.println("删除普通用户失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean updateUser(User user) {
        boolean flag = false;
        try {
            userDao.save(user);
            flag = true;
        }catch (Exception e){
            System.out.println("修改普通用户失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public User findUserById(Integer id) {
        try {
            Optional<User> user = userDao.findById(id);
            if (user.isPresent()){
                return user.get();
            }
        }catch (Exception e){
            System.out.println("查询失败！！！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    public User findByUsernameAndMail(String username, String mail) {
        return userDao.findByUsernameAndMail(username,mail);
    }

    @Override
    public User findUserByMail(String mail) {
        return userDao.findUserByMail(mail);
    }
}
